#!/bin/bash
if [ -d website/node_modules ]; then
  cd website
  npx lint-staged || exit $?
  cd ..
fi

if git diff --name-only --cached | grep -qE ".*\.rs$|(^|/)Cargo\.(lock|toml)$"; then
  cargo clippy --fix --all-targets --allow-staged --allow-dirty || exit $?
  cargo clippy -- --deny warnings || exit $?
  cargo fmt || exit $?
fi

cargo shear --fix || true

for rust_file in $(comm -12 <(git diff --name-only --cached | sort) <(git diff --name-only | sort) | grep ".*\.rs$"); do
  git add $rust_file
done
